package com.ruoyi.lvlian.controller;

import java.util.List;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.lvlian.dto.InvoiceDetailsDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.lvlian.domain.InvoiceDetails;
import com.ruoyi.lvlian.service.IInvoiceDetailsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 发票项目名称明细Controller
 *
 * @author ruoyi
 * @date 2024-07-26
 */
@Api(value = "税务发票条目 明细", tags = "税务发票条目接口")
@RestController
@RequestMapping("/invoiceDetails")
public class InvoiceDetailsController extends BaseController {
    @Autowired
    private IInvoiceDetailsService invoiceDetailsService;

    /**
     * 查询发票项目名称明细列表
     */
    //@RequiresPermissions("lvlian:invoiceDetails:list")
    @GetMapping("/list")
    public TableDataInfo list(InvoiceDetailsDto invoiceDetails) {
        startPage();
        List<InvoiceDetails> list = invoiceDetailsService.selectInvoiceDetailsList(invoiceDetails);
        return getDataTable(list);
    }

    /**
     * 导出发票项目名称明细列表
     */
/*    //@RequiresPermissions("lvlian:invoiceDetails:export")
    @Log(title = "发票项目名称明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvoiceDetails invoiceDetails)
    {
        List<InvoiceDetails> list = invoiceDetailsService.selectInvoiceDetailsList(invoiceDetails);
        ExcelUtil<InvoiceDetails> util = new ExcelUtil<InvoiceDetails>(InvoiceDetails.class);
        util.exportExcel(response, list, "发票项目名称明细数据");
    }*/

    /**
     * 获取发票项目名称明细详细信息
     */
    //@RequiresPermissions("lvlian:invoiceDetails:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(invoiceDetailsService.selectInvoiceDetailsById(id));
    }

    /**
     * 新增发票项目名称明细
     */
/*
    //@RequiresPermissions("lvlian:invoiceDetails:add")
    @Log(title = "发票项目名称明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InvoiceDetails invoiceDetails)
    {
        return toAjax(invoiceDetailsService.insertInvoiceDetails(invoiceDetails));
    }
*/
    private String validateInData(InvoiceDetails po) {
        if (StringUtils.isEmpty(po.getInvoiceNum())) {
            return "发票号码不能为空";
        }

        return "success";
    }

    @ApiOperation("新增  修改发票项目名称明细")
    //@RequiresPermissions("lvlian:invoiceDetails:edit")
    @Log(title = "发票项目名称明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated InvoiceDetails invoiceDetails) {

        if (!"success".equalsIgnoreCase(validateInData(invoiceDetails))) {
            return error(validateInData(invoiceDetails));
        }

        return toAjax(invoiceDetailsService.updateInvoiceDetails(invoiceDetails));
    }

    /**
     * 删除发票项目名称明细
     */
    @PostMapping("/remove")
    @Log(title = "删除发票项目名称明细", businessType = BusinessType.DELETE)
    public AjaxResult remove(@RequestBody String[] ids) {
        if( ids == null || ids.length == 0  ){
            return error("未选中") ;
        }
        return toAjax(invoiceDetailsService.deleteInvoiceDetailsByIds(ids));
    }
}
